package com.gitee.ipo.controller.system;


import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gitee.ipo.common.base.BaseController;
import com.gitee.ipo.common.base.BaseResult;
import com.gitee.ipo.system.entity.report.IpoReportInfo;
import com.gitee.ipo.system.entity.star.IpoDocUserStar;
import com.gitee.ipo.system.service.IpoReportInfoService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.frameworkset.util.annotations.PagerParam;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 系统用户表 前端控制器
 *
 * @author acao
 * @since 2024-02-08
 */
@Controller
@AllArgsConstructor
@RequestMapping("/report")
public class ReportController extends BaseController {

    private IpoReportInfoService ipoReportInfoService;

    /**
     * 访问页面：信披检索页面
     *
     * @return 跳转至列表页面
     */
    @RequestMapping("index")
    public String index() {
        return "system/report/index";
    }

    /**
     * 查询行业报告
     *
     * @param page 当前页
     * @return
     */
    @PostMapping(value = "/getReportInfo")
    @ResponseBody
    public BaseResult getReportInfo(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam("keyword") String keyword) {
        QueryWrapper<IpoReportInfo> wrapper = new QueryWrapper<>();
        wrapper.or().like(true, "title", keyword)
                .or().like(true, "abstracts", keyword)
                .orderBy(true, false, "up_date");

        IPage<IpoReportInfo> ipage = new Page<>(page, 20);
        IPage<IpoReportInfo> starIPage = ipoReportInfoService.page(ipage, wrapper);
        List<IpoReportInfo> records = starIPage.getRecords();
        if (!CollectionUtils.isEmpty(records)) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            List<IpoReportInfo> collect = records.stream()
                    .map(k -> k.setUpDateStr(simpleDateFormat.format(k.getUpDate())))
                    .peek(k -> {
                        if (StringUtils.isBlank(keyword)) {
                            return;
                        }
                        String title = k.getTitle();
                        String abstracts = k.getAbstracts();
                        if (title.contains(keyword)) {
                            String redKeyword = "<font style='color:red;'>" + keyword + "</font>";
                            k.setTitle(title.replaceAll(keyword, redKeyword));
                        }
                        if (abstracts.contains(keyword)) {
                            String redKeyword = "<font style='color:red;'>" + keyword + "</font>";
                            k.setAbstracts(abstracts.replaceAll(keyword, redKeyword));
                        }
                    })
                    .collect(Collectors.toList());
            return success("查询成功！", collect, (int) starIPage.getPages());
        } else {
            return error("信息不存在");
        }
    }

    /**
     * 文件下载
     *
     * @param id 文件下载路径
     * @return
     */
    @RequestMapping(value = "/downloadFile")
    public void downloadFile(HttpServletResponse response, HttpServletRequest request, Long id) {
        ipoReportInfoService.downloadFile(id, response, request);
    }


}
